package ca.kooki.codesnippet;

import junit.framework.Assert;

import org.junit.Test;

public class CodeSnippetTest 
{

  
  @Test
  public void Constructor()
  {
    CodeSnippet snippet = new CodeSnippet("t","k1 k2 k3","void(0);");
    
    Assert.assertEquals("t",snippet.getTitle());
    Assert.assertEquals("k1 k2 k3",snippet.getKeywords());
    Assert.assertEquals("void(0);",snippet.getCode());
    
    String[] allKeywords = snippet.getKeywordList();
    Assert.assertEquals(3, allKeywords.length);
    Assert.assertEquals("k1", allKeywords[0]);
    Assert.assertEquals("k2", allKeywords[1]);
    Assert.assertEquals("k3", allKeywords[2]);
    
  }
  
  @Test
  public void IgnoreKeywordWhiteSpace()
  {
    CodeSnippet snippet = new CodeSnippet("t"," k1   k2 k3  ","void(0);");

    String[] allKeywords = snippet.getKeywordList();
    Assert.assertEquals(3, allKeywords.length);
    Assert.assertEquals("k1", allKeywords[0]);
    Assert.assertEquals("k2", allKeywords[1]);
    Assert.assertEquals("k3", allKeywords[2]);

  }
  
  @Test
  public void ToStringShowsAll()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    Assert.assertEquals("t;k;c", snippet.toString());
    
  }
  
  @Test
  public void NotEqualsNull()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    Assert.assertFalse(snippet.equals(null));
    Assert.assertFalse(snippet.equals("blah"));
  }

  @Test
  public void NotEqualsWrongData()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    CodeSnippet compareTo = null;
    
    compareTo = new CodeSnippet("t1","k","c");
    Assert.assertFalse(snippet.equals(compareTo));

    compareTo = new CodeSnippet("t","k1","c");
    Assert.assertFalse(snippet.equals(compareTo));

    compareTo = new CodeSnippet("t","k","c1");
    Assert.assertFalse(snippet.equals(compareTo));

  }

  @Test
  public void NotEqualsWrongId()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    CodeSnippet compareTo = null;
    
    compareTo = new CodeSnippet("t","k","c");
    
    snippet.setId("10");
    compareTo.setId("11");
    Assert.assertFalse(snippet.equals(compareTo));
  }

  @Test
  public void EqualsSameIdIgnoresOtherData()
  {
    CodeSnippet snippet = new CodeSnippet("t1","k1","c1");
    CodeSnippet compareTo = new CodeSnippet("t2","k2","c2");
    
    snippet.setId("10");
    compareTo.setId("10");
    
    Assert.assertTrue(snippet.equals(compareTo));
  }

  
  @Test
  public void EqualsAllTheSame()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    CodeSnippet compareTo = new CodeSnippet("t","k","c");
    Assert.assertTrue(snippet.equals(compareTo));
  }

  @Test
  public void EqualsOneWithoutAnId()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    CodeSnippet compareTo = new CodeSnippet("t","k","c");
    
    snippet.setId("10");
    
    Assert.assertTrue(snippet.equals(compareTo));
    Assert.assertTrue(compareTo.equals(snippet));
  }

  
  @Test
  public void IsOfficial()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    Assert.assertEquals(false, snippet.isOfficial());
    snippet.setId("10");
    Assert.assertEquals(true, snippet.isOfficial());
  }
  
  @Test(expected=UnsupportedOperationException.class)
  public void getIdNotSetYet()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    snippet.getId();
  }

  @Test
  public void cannotSetIdToSameNumberMultipleTimes()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    snippet.setId("10");
    snippet.setId("10");
    Assert.assertEquals("10", snippet.getId());
  }

  
  @Test(expected=UnsupportedOperationException.class)
  public void cannotSetIdToDifferentNumber()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    snippet.setId("10");
    snippet.setId("11");
  }

  
  @Test
  public void getIdOkay()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    snippet.setId("10");
    Assert.assertEquals("10", snippet.getId());
  }
  
  @Test
  public void isSameCodeAs()
  {
    CodeSnippet snippet = new CodeSnippet("t","k","c");
    CodeSnippet compareTo = null;
    
    compareTo = new CodeSnippet("t","k","c");
    Assert.assertEquals(true, snippet.isSameCodeAs(compareTo));

    snippet = new CodeSnippet("t","k"," c");
    compareTo = new CodeSnippet("t","k","c ");
    Assert.assertEquals(true, snippet.isSameCodeAs(compareTo));

    compareTo = new CodeSnippet("t2","k2","c");
    Assert.assertEquals(true, snippet.isSameCodeAs(compareTo));

    compareTo = new CodeSnippet("t","k","c2");
    Assert.assertEquals(false, snippet.isSameCodeAs(compareTo));
    
  }

}
